package org.minhnvt1.android.moviesearch;

import java.util.ArrayList;
import java.util.List;

import org.minhnvt1.android.moviesearch.model.Movie;
import org.minhnvt1.android.moviesearch.model.Person;
import org.minhnvt1.android.moviesearch.services.GenericSeeker;
import org.minhnvt1.android.moviesearch.services.MovieSeeker;
import org.minhnvt1.android.moviesearch.services.PersonSeeker;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MovieSearchActivity extends Activity {
	
	
	private EditText searchEditText;
	private RadioButton moviesSearchRadioButton;
	private RadioButton peopleSearchRadioButton;
	//private RadioGroup searchRadioGroup;
	private TextView searchTypeTextView;
	private Button btnSearch;
	
	private GenericSeeker<Movie> movieSeeker = new MovieSeeker();
	private GenericSeeker<Person> personSeeker = new PersonSeeker();
	
	private ProgressDialog progressDialog;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        this.fillAllViewsById();
        
        moviesSearchRadioButton.setOnClickListener(radioButtonListener);
        peopleSearchRadioButton.setOnClickListener(radioButtonListener);
        
        btnSearch.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String query = searchEditText.getText().toString();
				performSearch(query);
			}
		});
 
    }
    
    // find all view from layout
    private void fillAllViewsById(){
    	searchEditText = (EditText) findViewById(R.id.searchEditText);
    	moviesSearchRadioButton = (RadioButton) findViewById(R.id.movieSearchRadio);
    	peopleSearchRadioButton = (RadioButton) findViewById(R.id.peopleSearchRadio);
    	
    	searchTypeTextView = (TextView) findViewById(R.id.txtSearchType);
    	btnSearch = (Button) findViewById(R.id.btnSearch);
    }
    
    public void longToast(CharSequence message){
    	Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }
    
    private OnClickListener radioButtonListener = new OnClickListener() {
		
		public void onClick(View v) {
			RadioButton radioButton = (RadioButton) v;
			searchTypeTextView.setText(radioButton.getText());
		}
	};
	private void performSearch(String query){
		progressDialog = ProgressDialog.show(MovieSearchActivity.this, "Please wait...", "Retrieving data", true, true);
		
		if(moviesSearchRadioButton.isChecked()){
			PerformMovieSearchTask task = new PerformMovieSearchTask();
			task.execute(query);
			progressDialog.setOnCancelListener(new CancelTaskOnClickListener(task));
		}
		else if(peopleSearchRadioButton.isChecked()){
			PerformPersonSearchTask task = new PerformPersonSearchTask();
			task.execute(query);
			progressDialog.setOnCancelListener(new CancelTaskOnClickListener(task));
		}
	}
	private class CancelTaskOnClickListener implements OnCancelListener{
		private AsyncTask<?, ?, ?> task;
		public CancelTaskOnClickListener(AsyncTask<?, ?, ?> task){
			this.task = task;
		}
		
		public void onCancel(DialogInterface dialog) {
			if(task != null){
				task.cancel(true);
			}
		}
	}
	private class PerformMovieSearchTask extends AsyncTask<String, Void, ArrayList<Movie>>{

		@Override
		protected ArrayList<Movie> doInBackground(String... params) {
			String query = params[0];
			return movieSeeker.find(query);
		}
		
		@Override
		protected void onPostExecute(final ArrayList<Movie> result){
			runOnUiThread(new Runnable() {
				
				public void run() {
					if(progressDialog != null){
						progressDialog.dismiss();
						progressDialog = null;
					}
					
					/**
					for(Movie movie : result){
						longToast(movie.name + " - " + movie.rating);
					}
					*/
					Intent intent = new Intent(MovieSearchActivity.this, MovieListActivity.class);
					intent.putExtra("movies", result);
					startActivity(intent);
					
				}
			});
		}
		
	}
	private class PerformPersonSearchTask extends AsyncTask<String, Void, List<Person>>{

		@Override
		protected List<Person> doInBackground(String... params) {
			String query = params[0];
			return personSeeker.find(query);
		}
		
		@Override
		protected void onPostExecute(final List<Person> result){
			runOnUiThread(new Runnable() {
				public void run() {
					if(progressDialog != null){
						progressDialog.dismiss();
						progressDialog = null;
					}
					if(result != null){
						for(Person person : result){
							longToast(person.name);
						}
					}
				}
			});
		}
		
	}
}